package cn.itcast.tramsformation;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author KTL
 * @version V1.0
 * @Package cn.itcast.tramsformation
 * @date 2021/2/24 0024 10:51
 * @Copyright © 2015-04-29  One for each, and two for each
 *              演示Transformations-各种分区
 */
public class TransformationsDemo05 {
    public static void main(String[] args) throws Exception {
        //0,env
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        //1.source
        final DataStreamSource<String> lineDS = env.readTextFile("data/words.txt");
        final SingleOutputStreamOperator<Tuple2<String, Integer>> tupleDS = lineDS.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {

            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                final String[] s = value.split(" ");
                for (String words : s) {
                    out.collect(Tuple2.of(words, 1));
                }
            }
        });
        //2.transformation
        final DataStream<Tuple2<String, Integer>> result1 = tupleDS.global();  //发送到第一个task
        final DataStream<Tuple2<String, Integer>> result2 = tupleDS.broadcast();
        final DataStream<Tuple2<String, Integer>> result3 = tupleDS.forward();
        final DataStream<Tuple2<String, Integer>> result4 = tupleDS.shuffle();
        final DataStream<Tuple2<String, Integer>> result5 = tupleDS.rebalance();
        final DataStream<Tuple2<String, Integer>> result6 = tupleDS.rescale();
        final DataStream<Tuple2<String, Integer>> result7 = tupleDS.partitionCustom(new MyPartitioner(), t -> t.f0);

        //3.sink
        result1.print("result1");
        result2.print("result2");
        result3.print("result3");
        result4.print("result4");
        result5.print("result5");
        result6.print("result6");
        result7.print("result7");
        //4.execute
        env.execute();
    }


    /**
     * 自定义分区，这里可以对数据进行过滤
     */
    public static class MyPartitioner implements Partitioner<String>{
        @Override
        public int partition(String key, int numPartitions) {
            //if(key.equals("北京")) return 0 ; 这里写自己的分区逻辑即可
            return 0;
        }
    }

}
